package com.navi.loaddata.loadService.impl.arrayCfMappingData;

import com.navi.loaddata.core.etlException.ETLException;
import com.navi.loaddata.core.gpEntity.hisToGP.ProductHis;
import com.navi.loaddata.core.mesEntity.MesP1MESDB;
import com.navi.loaddata.core.mysqlEntity.EtlConfD;
import com.navi.loaddata.core.oracleEntity.fileMapping.CellArrayCfMappingT;
import com.navi.loaddata.core.oracleEntity.fileMapping.CellArrayCfMappingTModel;
import com.navi.loaddata.core.repository.impl.OracleRepositoryImpl;
import com.navi.loaddata.core.util.LoggerUtil;
import com.navi.loaddata.loadDao.SaveMappingDao;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author xufan
 */
@Service
@Transactional(transactionManager = "thirdaryPlatformTransactionManager",
        rollbackFor = {RuntimeException.class, Exception.class})
public class LoadArrayCfMappingDataService {
    private static final Logger logger = LoggerFactory.getLogger(LoadArrayCfMappingDataService.class);
    private LoggerUtil loggerUtil = new LoggerUtil(this.getClass());

    @Autowired
    private ProcessArrayCfMappingDataService processArrayCfMappingDataService;
    @Autowired
    private LoadArrayCfMappingInfoService loadArrayCfMappingInfoService;
    @Autowired
    private SaveMappingDao saveMappingDao;

    public String[] loadArrayCfMappingDataMain(EtlConfD etlConfD) throws Exception {
        String[] resMsg = loadArrayCfMappingData(etlConfD);
        logger.info("LoadArrayCfMappingDataMain finish");
        return resMsg;
    }


    private String[] loadArrayCfMappingData(EtlConfD etlConfD) throws Exception {
        String[] resMsg = new String[3];

        // get L2100 glass
        List<CellArrayCfMappingTModel> glassList = loadArrayCfMappingInfoService.loadArrayCfInfo(etlConfD);
        logger.info("{} L2100 glass count {}", etlConfD.getShop(), glassList.size());
        if (!glassList.isEmpty()) {
            //process
            List<CellArrayCfMappingT> cellList = processArrayCfMappingDataService.processArrayCfMapping(etlConfD, glassList);

            resMsg[0] = String.valueOf(cellList.size());
            resMsg[1] = "0";

            //save
            for (int i = 0; i < cellList.size(); i += 1000) {
                List<CellArrayCfMappingT> cellList1000 = new ArrayList<>();
                StringBuffer glassIds = new StringBuffer();
                for (int j = 0; j < 1000; j++) {
                    if (i + j < cellList.size()) {
                        glassIds.append("'" + cellList.get(i + j).getArrayComponentId() + "',");
                        cellList1000.add(cellList.get(i + j));
                    }
                }
                glassIds.deleteCharAt(glassIds.length()-1);

                saveMappingDao.saveAryCfMapping(cellList1000,glassIds);
            }
        }
        return resMsg;
    }


}
